package com.slipper.system.dictionary.service;

import com.slipper.common.util.Tool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.dictionary.dao.DictionaryDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.slipper.system.api.dictionary.bo.DictionaryBO;
import com.slipper.common.transaction.ReadTransactional;
import com.slipper.common.transaction.WriteTransactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
@ReadTransactional
public class DictionaryService {
    @Resource
    private DictionaryDao dictionaryDao;
    
    public DictionaryBO queryBy(String id){
        return dictionaryDao.queryBy(id);
    }
    
    public DictionaryBO queryByCode(String code){
        return dictionaryDao.queryByCode(code);
    }
    
    public PageInfo<DictionaryBO> queryPage(DictionaryBO bo){
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dictionaryDao.queryList(bo));
    }
    
    @WriteTransactional
    public Integer save(DictionaryBO bo){
        int result;
        if (bo.getId() != null && !"".equals(bo.getId())) {
            result = this.dictionaryDao.update(bo);
        } else {
            bo.setId(Tool.getUUID());
            result = this.dictionaryDao.insert(bo);
        }
        
        return result;
    }
    
    @WriteTransactional
    public Integer delete(DictionaryBO bo){
        return dictionaryDao.delete(bo);
    }
    
    public LinkedList<Map<String, String>> queryDicData(String dicCode) {
        List<Map<String, Object>> list = this.dictionaryDao.queryDictData(dicCode);
        LinkedList<Map<String, String>> result = new LinkedList<>();
        for (Map<String, Object> temp : list) {
            Map<String, String> data = new HashMap<>();
            data.put("key", String.valueOf(temp.get("dicKey")));
            data.put("value", String.valueOf(temp.get("dicValue")));
            result.add(data);
        }
        return result;
    }

    public Map<String, List<Map<String, String>>> queryAllDicData() {
        List<Map<String, Object>> list = this.dictionaryDao.queryDictData(null);
        Map<String, List<Map<String, String>>> resultList = new HashMap<>();
        
        for (Map<String, Object> temp : list) {
            List<Map<String, String>> result = resultList.computeIfAbsent(
                    (String) temp.get("code"), k -> new LinkedList<>());

            Map<String, String> data = new HashMap<>();
            data.put("key", String.valueOf(temp.get("dicKey")));
            data.put("value", String.valueOf(temp.get("dicValue")));
            result.add(data);
        }
        
        List<Map<String, Object>> dictList = this.dictionaryDao.queryValidDictList();
        List<Map<String, String>> result = new LinkedList<>();
        for (Map<String, Object> temp : dictList) {
            Map<String, String> data = new HashMap<>();
            data.put("key", String.valueOf(temp.get("dicKey")));
            data.put("value", String.valueOf(temp.get("dicValue")));
            result.add(data);
        }
        resultList.put("dicDictList", result);
        return resultList;
    }

    public List<DictionaryBO> queryList(DictionaryBO bo) {
        return this.dictionaryDao.queryList(bo);
    }

    public Map<Object, Object> querySingleDicData(String dicCode) {
        List<Map<String, Object>> list = this.dictionaryDao.queryDictData(dicCode);
        Map<Object, Object> result = new HashMap<>();
        for (Map<String, Object> temp : list) {
            result.put(String.valueOf(temp.get("dicKey")), String.valueOf(temp.get("dicValue")));
        }
        return result;
    }

    public String queryDicValue(String dicCode, String dicKey) {
        Map<Object, Object> data = querySingleDicData(dicCode);
        if (data.isEmpty() || data.get(dicKey) == null) {
            return null;
        }

        return data.get(dicKey).toString();
    }
}
